package com.ruoyi.web.controller.dj;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.vo.DensVO;
import com.ruoyi.common.exception.ProcessStopException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SseEmitterUtils;
import com.ruoyi.dj.domain.GlobalDensitySimulateFutureParam;
import com.ruoyi.dj.domain.GlobalDensitySimulateNewParam;
import com.ruoyi.dj.domain.GlobalDensitySimulateParam;
import com.ruoyi.dj.service.AtmDensityCalculationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * </p>
 *
 * @author liuhl
 * @since 2024-06-11
 */

@Slf4j
@RestController
@RequestMapping("/dj/atmDensity")
@Api(value = "AtmDensityCalculationController", tags = "多模型计算与预报-大气密度计算")
public class AtmDensityCalculationController extends BaseController {

    @Resource
    private AtmDensityCalculationService atmDensityCalculationService;



    //@PreAuthorize("@ss.hasPermi('dj:atmDensity:getGlobalTatestTime')")
    @ApiOperation(value = "获取大气密度全球分布最新时间", notes = "获取大气密度全球分布最新时间")
    @GetMapping("/getGlobalTatestTime")
    public AjaxResult getGlobalTatestTime() {
        return atmDensityCalculationService.getGlobalTatestTime();
    }

    //@PreAuthorize("@ss.hasPermi('dj:atmDensity:getGlobalHistData')")
    @ApiOperation(value = "获取大气密度全球分布模拟数据", notes = "获取大气密度全球分布模拟数据")
    @GetMapping("/getGlobalHistData")
    public AjaxResult getGlobalHistData(GlobalDensitySimulateParam param
    ) throws ProcessStopException {
        return atmDensityCalculationService.getGlobalHistData(param);
    }

    @ApiOperation(value = "新-获取大气密度全球分布模拟数据", notes = "新-获取大气密度全球分布模拟数据")
    @GetMapping("/getGlobalHistDataNew")
    public AjaxResult getGlobalHistDataNew(@Validated GlobalDensitySimulateNewParam param
    ) throws ProcessStopException {
        try {
            return atmDensityCalculationService.getGlobalHistDataNew(param);
        }finally {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            SseEmitterUtils.sendMessage(param.getSseId()+loginUser.getUsername(),"100");
            SseEmitterUtils.remove(param.getSseId() + loginUser.getUsername());
        }

    }

    @ApiOperation(value = "大气密度全球分布模拟-未来时期计算", notes = "未来时期模拟计算")
    @PostMapping("/futureCal")
    public R<List<DensVO>> futureCal(@RequestBody GlobalDensitySimulateFutureParam param) throws ProcessStopException {
        try {
            List<DensVO> commonVOS = atmDensityCalculationService.futureCal(param);
            return R.ok(commonVOS);
        }finally {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            SseEmitterUtils.sendMessage(param.getSseId()+loginUser.getUsername(),"100");
            SseEmitterUtils.remove(param.getSseId() + loginUser.getUsername());
        }
    }

    //@PreAuthorize("@ss.hasPermi('dj:atmDensity:addGlobalHistData')")
    @ApiOperation(value = "大气密度全球分布模拟入库", notes = "大气密度全球分布模拟入库")
    @PostMapping("/addGlobalHistData")
    public AjaxResult addGlobalHistData(@RequestBody GlobalDensitySimulateNewParam param) {
        return atmDensityCalculationService.addGlobalHistData(param);
    }
    //@PreAuthorize("@ss.hasPermi('dj:atmDensity:getOrbitSimulateHist')")
    @ApiOperation(value = "获取轨道大气密度模拟数据", notes = "获取轨道大气密度模拟数据")
    @GetMapping("/getOrbitSimulateHist")
        public AjaxResult getOrbitSimulateHist(@ApiParam(value = "卫星名称", name = "satName",required = true) @RequestParam("satName") String satName,
                              @ApiParam(value = "开始时间（yyyy-MM-dd HH:mm:ss）", name = "startTime",required = true) @RequestParam("startTime") String startTime,
                              @ApiParam(value = "结束时间（yyyy-MM-dd HH:mm:ss）", name = "endTime",required = true) @RequestParam("endTime") String endTime,
                              @ApiParam(value = "要素", name = "element",required = true) @RequestParam("element") String element,
                              @ApiParam(value = "大气模型", name = "model",required = true) @RequestParam("model") String model,
                              @ApiParam(value = "时间间隔单位分钟", name = "interval") @RequestParam(value = "interval",required = false) Integer interval,
                              @ApiParam(value = "水平分辨率", name = "resolution") @RequestParam(value = "resolution",required = false) String resolution,
                              @ApiParam(value = "通讯ID", name = "sseId",required = true) @RequestParam("sseId") String sseId
    ) throws ProcessStopException {
        return atmDensityCalculationService.getOrbitSimulateHist(satName, startTime, endTime, element, model, interval, resolution, sseId);
    }


    //@PreAuthorize("@ss.hasPermi('dj:atmDensity:slideAvg')")
    @ApiOperation(value = "获取滑动平均数据", notes = "获取滑动平均数据")
    @PostMapping("/slideAvg")
    public AjaxResult slideAvg(@RequestBody Map<String,Object> paramMap) {
        log.info("paramMap:{}", paramMap);
        List<String> pathList = (List<String>)paramMap.get("pathList");
        return atmDensityCalculationService.slideAvg(pathList);
    }


    //@PreAuthorize("@ss.hasPermi('dj:atmDensity:getDensityProfile')")
    @ApiOperation(value = "获取大气密度廓线模拟数据", notes = "获取大气密度廓线模拟数据")
    @GetMapping("/getDensityProfile")
    public AjaxResult getDensityProfile(@ApiParam(value = "时间（yyyy-MM-dd HH:mm:ss）", name = "time",required = true) @RequestParam("time") String time,
                                           @ApiParam(value = "开始高度", name = "startAlt",required = true) @RequestParam("startAlt") String startAlt,
                                           @ApiParam(value = "结束高度", name = "endAlt",required = true) @RequestParam("endAlt") String endAlt,
                                           @ApiParam(value = "高度间隔", name = "interval",required = true) @RequestParam("interval") Integer interval,
                                           @ApiParam(value = "经度", name = "lon",required = true) @RequestParam("lon") Double lon,
                                           @ApiParam(value = "纬度", name = "lat",required = true) @RequestParam("lat") Double lat,
                                           @ApiParam(value = "模型", name = "model",required = true) @RequestParam("model") String model
    ) {
        return atmDensityCalculationService.getDensityProfile(time, startAlt, endAlt, interval, lon, lat, model);
    }
    //@PreAuthorize("@ss.hasPermi('dj:atmDensity:subscribe')")
    @ApiOperation(value = "建立连接", notes = "建立连接")
    @GetMapping(path = "subscribe", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public SseEmitter push(String id, String flag) {
        return atmDensityCalculationService.push(id, flag);
    }

    //@PreAuthorize("@ss.hasPermi('dj:atmDensity:getModelList')")
    @ApiOperation(value = "获取大气密度模型列表数据", notes = "获取大气密度模型列表数据")
    @GetMapping("/getModelList")
    public AjaxResult getModelList(
                                   @ApiParam(value = "开始高度", name = "startAlt",required = true) @RequestParam("startAlt") String startAlt,
                                   @ApiParam(value = "结束高度", name = "endAlt",required = true) @RequestParam("endAlt") String endAlt,
                                   @ApiParam(value = "开始时间（yyyy-MM-dd HH:mm:ss）", name = "startTime",required = true) @RequestParam("startTime") String startTime,
                                   @ApiParam(value = "结束时间（yyyy-MM-dd HH:mm:ss）", name = "endTime",required = true) @RequestParam("endTime") String endTime) {
        return atmDensityCalculationService.getModelList(startAlt,endAlt,startTime,endTime);
    }

    //@PreAuthorize("@ss.hasPermi('dj:atmDensity:getModelBySatellite')")
    @ApiOperation(value = "根据卫星大气密度模型列表", notes = "根据卫星大气密度模型列表")
    @GetMapping("/getModelBySatellite")
    public AjaxResult getModelBySatellite(
            @ApiParam(value = "开始时间（yyyy-MM-dd HH:mm:ss）", name = "startTime",required = true) @RequestParam("startTime") String startTime,
            @ApiParam(value = "结束时间（yyyy-MM-dd HH:mm:ss）", name = "endTime",required = true) @RequestParam("endTime") String endTime,
            @ApiParam(value = "卫星名称", name = "satName",required = true) @RequestParam("satName") String satName) {
        return atmDensityCalculationService.getModelBySatellite(startTime,endTime,satName);
    }

    //@PreAuthorize("@ss.hasPermi('dj:atmDensity:getTimeRange')")
    @ApiOperation(value = "根据卫星获取时间范围", notes = "根据卫星获取时间范围")
    @GetMapping("/getTimeRange")
    public AjaxResult getTimeRange(@ApiParam(value = "卫星名称", name = "satName",required = true) @RequestParam("satName") String satName) {
        return atmDensityCalculationService.getTimeRange(satName);
    }

    @ApiOperation(value = "大气密度分布模拟 获取时间范围", notes = "大气密度分布模拟 获取时间范围")
    @GetMapping("/densTimeRange")
    public R<List<Date>> densTimeRange(@ApiParam(value = "历史 history 或 未来 future时间", name = "type",required = false) @RequestParam(required = false) String type) {
        List<Date> res = atmDensityCalculationService.densTimeRange(type);
        return R.ok(res);
    }

    @ApiOperation(value = "大气密度廓线 获取时间范围", notes = "大气密度廓线 获取时间范围")
    @GetMapping("/outTimeRange")
    public  R<List<Date>> outTimeRange() {
        List<Date> res = atmDensityCalculationService.outTimeRange();
        return R.ok(res);
    }

    //@PreAuthorize("@ss.hasPermi('dj:atmDensity:getProfileTimeRange')")
    @ApiOperation(value = "获取大气密度廓线模拟时间范围", notes = "获取大气密度廓线模拟时间范围")
    @GetMapping("/getProfileTimeRange")
    public AjaxResult getProfileTimeRange() {
        return atmDensityCalculationService.getProfileTimeRange();
    }


}
